* RUNS ALL THE DID-ANALYSES FOR THE INMOVERS



* * * * * PLOT GRAPHS OF THE TAX VARIATION (1ST STAGE) * * * * *

* Collapse by different regressive tax brackets and make graphs of NTR variation

cd "$mypathRR/Results/"

local b "  60  "
local t "  80  "
local n : word count `b'

forvalues i = 1/`n' {
local x : word `i' of `b'
local y : word `i' of `t'
cap drop treat_i
gen treat_i=.
replace treat_i=0 if ctrl_ir_`x'_`y'==1
replace treat_i=1 if taxinc_thre2==1
label var treat_i "Income tax treatment"

preserve

sort persid zuzugjahre year
keep if freq==1
drop if year>2010
drop if zuzugjahre>2010


* graphs of the tax variation
cap binscatter avg_ntr zuzugjahre , rd(2005.5 2007.5) by(treat_i) ///
ytitle("Average net-of-tax rate") xtitle(" ") legend( label(1 "control") label(2 "treated") ) ///
savegraph("Fig_7b)-avgntr_Incomers_inctreat_`x'_`y'_uw.pdf") replace

restore

}



*************************************************************************************************

* REGRESSIONS *
// specification with 1 treatment (2006), dropping all obs after 2007

cap rm "$mypathRR/Results/appendixtables/B4_appendixtable.xls"
cap rm "$mypathRR/Results/appendixtables/B4_appendixtable.txt"

cd "$mypathRR/Results/Inmover/results_01-07"


* INCLUDE YEARS UP TO 2007 AND INSTRUMENT 2006 REFORM

local b " 60 55"
local t " 80 75"
local n : word count `b'

forvalues i = 1/`n' {
local x : word `i' of `b'
local y : word `i' of `t'
cap drop treat_i
gen treat_i=.
replace treat_i=0 if ctrl_ir_`x'_`y'==1
replace treat_i=1 if taxinc_thre2==1
label var treat_i "Income tax treatment"



preserve
sort persid zuzugjahre year
keep if freq==1
drop if year>2007
drop if zuzugjahre>2007


collapse (count) Inmovers=persid (mean) avgtt=avgt_stek_taxable (mean) avgtg=avgt_stek_gross (mean) mtr=mtr_total , by(zuzugjahre treat_i)
label var Inmovers "Incomers"
label var avgtt "Average tax rate on taxable income"
label var avgtg "Average tax rate on gross income"
label var mtr "Marginal tax rate on taxable income"
label var treat_i "Treatment"
label var zuzugjahre "Year"

xtset treat_i zuzugjahre
tsfill, full
replace Inmovers=0 if Inmovers==.

gen Period=(zuzugjahre>=2006)
gen DiD=treat_i*Period
gen weight=Inmovers

* for semi-elasticities: average change in tax for treatment group
drop if DiD==.
sort treat_i zuzugjahre

bys treat_i: egen tax_0=mean(avgtt) if zuzugjahre<2006
bys treat_i: egen tax_1=mean(avgtt) if zuzugjahre>2005 & zuzugjahre<2008
bys treat_i (zuzugjahre): replace tax_0=tax_0[_n-1] if tax_0==.
bys treat_i (zuzugjahre): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (zuzugjahre): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (zuzugjahre): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (zuzugjahre): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (zuzugjahre): replace tax_1=tax_1[_n+1] if tax_1==.

gen delta_tax=tax_1-tax_0 if treat_i==1
sum delta_tax
local delta_tax = abs(`r(mean)')
local delta_tax2 = `delta_tax'*100
global delta_tax_2006_`x'_`y' = `delta_tax2'

gen delta_tax_percent = delta_tax/tax_0
sum delta_tax_percent
local delta_tax_percent = abs(`r(mean)')
local delta_tax_percent2 = `delta_tax_percent'*100
global delta_tax_pct_2006_`x'_`y' = `delta_tax_percent2'




*  REDUCED FORM DID REGRESSION IN LEVELS 
display _newline(5)
display "Reduced Form, Control group: `x'-`y'%"

reg Inmovers DiD treat_i  ib2005.zuzugjahre, vce(robust)
eststo did_level_`x'_`y'

scalar define semi_e = _b[DiD]/`delta_tax'/100
global semi_e_2006_`x'_`y' = semi_e
 
 
 		local did_`x'_`y'_B = _b[DiD]
		local se_`x'_`y'_B = _se[DiD]
		local pval = 2*(1-normal(abs(_b[DiD]/_se[DiD])))
			display "`pval'"
			gen star="" 
			replace star="*" in 1 if `pval'<.1 &`pval'>=.05 //print star if p-value between
			replace star="**"  in 1 if `pval'<.05 &`pval'>=.01
			replace star="***"  in 1 if `pval'<.01
			local p = star[1]
				display "`p'"
		local did_`x'_`y'_B = trim("`: display %5.2f `did_`x'_`y'_B''")
		global DiD_`x'_`y'_B = "`did_`x'_`y'_B'`p'"
		global SE_`x'_`y'_B= trim("`: display %5.2f `se_`x'_`y'_B''")
			drop star
		display "${DiD_`x'_`y'_B}"
		

		
// REGRESSION TABLE FOR APPENDIX
if `x' == 60 & `y' ==80 { 
	outreg2 using "$mypathRR/Results/appendixtables/B4_appendixtable.xls", excel label  e(F) dec(2) replace ///
	sortvar(DiD lnnet_avgtt treat_i i.zuzugjahre ) ctitle(" ", " ", "Reduced", "(level)") title("DiD Estimations Incomers only, Control Group: `x'-`y'% of Threshold") ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80} )			
	}		
	
	
	
		
*  REDUCED FORM DID REGRESSION IN LOGS 
display _newline(5)
display "Reduced Form (in logs), Control group: `x'-`y'%"

replace Inmovers = ln(Inmovers)

reg Inmovers DiD  treat_i  ib2005.zuzugjahre , vce(robust)
eststo did_log_`x'_`y'

 		local did_`x'_`y'_C = _b[DiD]
		local se_`x'_`y'_C = _se[DiD]
		local pval = 2*(1-normal(abs(_b[DiD]/_se[DiD])))
			display "`pval'"
			gen star="" 
			replace star="*" in 1 if `pval'<.1 &`pval'>=.05 //print star if p-value between
			replace star="**"  in 1 if `pval'<.05 &`pval'>=.01
			replace star="***"  in 1 if `pval'<.01
			local p = star[1]
				display "`p'"
		local did_`x'_`y'_C = trim("`: display %5.3f `did_`x'_`y'_C''")
		global DiD_`x'_`y'_C = "`did_`x'_`y'_C'`p'"
		global SE_`x'_`y'_C= trim("`: display %5.3f `se_`x'_`y'_C''")
			drop star

			
// REGRESSION TABLE FOR APPENDIX
if `x' == 60 & `y' == 80 { 
	outreg2  using "$mypathRR/Results/appendixtables/B4_appendixtable.xls", excel label  e(F) dec(2) append ///
	sortvar(DiD lnnet_avgtt treat_i i.zuzugjahre ) ctitle(" ", " ", "Reduced", "(log)") ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80} )
}			




			
* 2SLS - IN LOGS TO OBTAIN THE ELASTICITY
gen lnnet_avgtt = ln(1-avgtt)
label var lnnet_avgtt "$\eta^F$"

	// HAUSMAN TEST (put this first, to include results in regression tables
	* 	Hypothesis H0: tax rates are exogenous
	*	Note:  	1) if all vars are exogenous, both 2sls and ols are consistent
	*			2) if all vars are exogenous, ols is more efficient, but it's inconsistent if violated
quiet{
	foreach var in lnnet_avgtt {
		ivreg2 Inmovers treat_i   ib2005.zuzugjahre (`var' = DiD)   
		eststo iv_`var'

		reg Inmovers `var' treat_i  ib2005.zuzugjahre 
		eststo ols_`var'

		hausman iv_`var' ols_`var'
		scalar chi07_`x'_`y' = r(chi2)
		scalar p07_`x'_`y' = r(p)
	} 
}

display _newline(5)
display "IV-Regression, AVERAGE NTR, Control group: `x'-`y'%"

ivreg2 Inmovers treat_i  ib2005.zuzugjahre (lnnet_avgtt = DiD) , first savefirst savefprefix(_1st_`x'_`y'_) redundant(DiD)  robust
est sto iv07_`x'_`y'

di e(sstatp)
di e(archi2p)


//statistics to be saved in first-stage table:
display "first stage statistics"
// empty the locals
local Fstat
local Fstat_alt1
local Fstat_alt2
local widstat
local idstat
local idp
local esstat
local estatp
local arf
local arfp
local archi2
local archi2p
local sstat
local sstatp

// save stats as locals with nice display format
local idstat 	= trim("`: display %5.2f e(idstat)'")
local idp 		= trim("`: display %5.3f e(idp)'")
local esstat 	= trim("`: display %5.2f e(estat)'")
local estatp 	= trim("`: display %5.3f e(estatp)'")
local arf 		= trim("`: display %5.2f e(arf)'")
local arfp 		= trim("`: display %5.3f e(arfp)'")
local archi2 	= trim("`: display %5.2f e(archi2)'")
local archi2p	= trim("`: display %5.3f e(archi2p)'")
local sstat 	= trim("`: display %5.2f e(sstat)'")
local sstatp 	= trim("`: display %5.3f e(sstatp)'")
	
// F-Test
local widstat = trim("`: display %5.2f e(widstat)'") // F statistic for weak identification (Cragg-Donald or Kleibergen-Paap)
local Fstat_alt1 = trim("`: display %5.2f e(cdf)'") // should be the same as widstat, but not valid when errors are not iid! (e.g., robust)
local Fstat_alt2 = trim("`: display %5.2f e(F)'")	// overall F-test of 2nd stage
global Ftest_`x'_`y' = `widstat'
global Ftest2_`x'_`y' = `Fstat_alt1'
global Ftest3_`x'_`y' = `Fstat_alt2'

// compute p-value for F-Test widstat
	local df1 = e(ardf)
	local df2 = e(ardf_r)
	local F = e(widstat)

	display "These are the parameters for the F-Test: F( `df1', `df2', `F')"
	local pvalF = Ftail(`df1', `df2', `F')
		display "This is the F-stat pvalue for `x'-`y'% group: `pvalF'"
				gen starF = ""
				replace starF="*" in 1 if `pvalF'<.1 &`pvalF'>=.05 //print star if p-value between
				replace starF="**"  in 1 if `pvalF'<.05 &`pvalF'>=.01
				replace starF="***"  in 1 if `pvalF'<.01
				local pF = starF[1]
					display "Stars for F-stat pvalue for `x'_`y': `pF'"
		global Ftest_`x'_`y'_st = "`widstat'`pF'"
		local Ftest_pval = trim("`: display %5.3f `pvalF''")
		global Ftest_pval_`x'_`y' = `Ftest_pval'
				drop starF

// save coefficients, standard errors and stars for final output table
 		local se_`x'_`y'_E = _se[lnnet_avgtt]
		local e_`x'_`y'_E = _b[lnnet_avgtt]
		local pval = 2*(1-normal(abs(_b[lnnet_avgtt]/_se[lnnet_avgtt])))
			display "`pval'"
			gen star="" 
			replace star="*" in 1 if `pval'<.1 &`pval'>=.05 //print star if p-value between
			replace star="**"  in 1 if `pval'<.05 &`pval'>=.01
			replace star="***"  in 1 if `pval'<.01
			local p = star[1]
				display "Star P-Value: `p'"
		local e_`x'_`y'_E = trim("`: display %5.2f `e_`x'_`y'_E''")
		global E_`x'_`y'_E = "`e_`x'_`y'_E'`p'"
		global SE_`x'_`y'_E= trim("`: display %5.2f `se_`x'_`y'_E''")
			drop star

			
// REGRESSION TABLE FOR APPENDIX
if `x' == 60 & `y' ==80 { 
	outreg2 using "$mypathRR/Results/appendixtables/B4_appendixtable.xls", excel label  e(F) dec(3) append ///
	sortvar(DiD lnnet_avgtt treat_i i.zuzugjahre  ) ctitle(" ", " ", "2SLS", "(log)") ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80}, ///
			Hausman$ ^1$ , chi07_60_80, P-value, p07_60_80)
	}
	

estimates restore _1st_`x'_`y'_lnnet_avgtt
outreg2  using "DiD_Incomers_inctreat_first_`x'_`y'_uw.xls",  excel label replace ///
  addstat(	/*"Cragg-Donald F statistic", `Fstat_alt1', "F", `Fstat_alt2',*/ ///
   			"Weak identification F test$^a$", `widstat', "\hspace{0.5cm}\textit{P-value}", `Ftest_pval', ///
            "Underidentification test$^b$", `idstat', "\hspace{0.5cm}\textit{P-value}", `idp' , ///
			 "$\chi^2$ test  significance endog. regressors$^c$", `archi2' , "\hspace{0.5cm}\textit{P-value}", `archi2p' ,  ///
			 "S statistic significance endog. regressors$^d$", `sstat', "\hspace{0.5cm}\textit{P-value}", `sstatp') ///
			 /*"Anderson-Rubin F-test of significance of endogenous regressors", `arf', "\hspace{0.5cm}\textit{P-value}", `arfp',*/ ///
			 /*"Endogeneity test (C statistic or difference-in-Sargan stat)", `esstat', "\hspace{0.5cm}\textit{P-value}", `estatp'*/  ///
        addnote("Robust standard errors in parentheses.") ///
		keep(lnnet_avgtt DiD ib2005.zuzugjahre treat_i ) ctitle("2001-07", "Inmovers", "`x'-`y'\%")  


* OLS MODEL, NO INSTRUMENT FOR TAX RATE
display _newline(5)
display "OLS Model (no IV), avg. NTR, control group: `x'-`y'%"  
reg Inmovers lnnet_avgtt treat_i  ib2005.zuzugjahre, vce(robust)
eststo ols07_`x'_`y'

// REGRESSION TABLE FOR APPENDIX
if `x' == 60 & `y' == 80 { 
	outreg2 using "$mypathRR/Results/appendixtables/B4_appendixtable.xls", excel label  e(F) dec(3) append ///
	sortvar(DiD lnnet_avgtt treat_i i.zuzugjahre ) ctitle(" ", " ", "OLS", "(log)") ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80})
}
restore

cap rm "DiD_Incomers_inctreat_first_`x'_`y'_uw.txt"
cap rm "DiD_Resultstable_Inmovers_`x'_`y'_uw.txt"
cap rm "DiD_Incomers_inctreat_`x'_`y'_uw.txt"
}





*************************************************************************************************

* REGRESSIONS *
// specification with 2 treatments (2006 and 2008)

cd "$mypathRR/Results/Inmover/results_01-10"

local b " 60 55"
local t " 80 75"
local n : word count `b'

forvalues i = 1/`n' {
local x : word `i' of `b'
local y : word `i' of `t'
cap drop treat_i
gen treat_i=.
replace treat_i=0 if ctrl_ir_`x'_`y'==1
replace treat_i=1 if taxinc_thre2==1
label var treat_i "Income tax treatment"


preserve
sort persid zuzugjahre year
keep if freq==1
drop if year>2010
drop if zuzugjahre>2010


collapse (count) Inmovers=persid (mean) avgtt=avgt_stek_taxable (mean) avgtg=avgt_stek_gross (mean) mtr=mtr_total , by(zuzugjahre treat_i)
label var Inmovers "Incomers"
label var avgtt "Average tax rate on taxable income"
label var avgtg "Average tax rate on gross income"
label var mtr "Marginal tax rate on taxable income"
label var treat_i "Treatment"
label var zuzugjahre "Year"

xtset treat_i zuzugjahre
tsfill, full
replace Inmovers=0 if Inmovers==.


gen Period1=0
replace Period1=1 if zuzugjahre>=2006&zuzugjahre<2008
gen DiD1=treat_i*Period1

gen Period2=0
replace Period2=1 if zuzugjahre>=2008
gen DiD2=treat_i*Period2

gen weight=Inmovers

* for the semi-elasticities: average change in tax for treatment group
sort treat_i zuzugjahre

bys treat_i: egen tax_0=mean(avgtt) if zuzugjahre<2006
bys treat_i: egen tax_1=mean(avgtt) if zuzugjahre>2005 & zuzugjahre<2008
bys treat_i: egen tax_2=mean(avgtt) if zuzugjahre>2007 
bys treat_i (zuzugjahre): replace tax_0=tax_0[_n-1] if tax_0==.
bys treat_i (zuzugjahre): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (zuzugjahre): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (zuzugjahre): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (zuzugjahre): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (zuzugjahre): replace tax_1=tax_1[_n+1] if tax_1==.
bys treat_i (zuzugjahre): replace tax_2=tax_2[_n+1] if tax_2==.
bys treat_i (zuzugjahre): replace tax_2=tax_2[_n+1] if tax_2==.
bys treat_i (zuzugjahre): replace tax_2=tax_2[_n+1] if tax_2==.
bys treat_i (zuzugjahre): replace tax_2=tax_2[_n+1] if tax_2==.
bys treat_i (zuzugjahre): replace tax_2=tax_2[_n+1] if tax_2==.

gen delta_tax_06=tax_1-tax_0 if treat_i==1
sum delta_tax_06
local delta_tax_06 = abs(`r(mean)')
local delta_tax_062 = `delta_tax_06'*100
global delta_tax_2006_`x'_`y' = `delta_tax_062'

gen delta_tax_percent = delta_tax_06/tax_0
sum delta_tax_percent
local delta_tax_percent = abs(`r(mean)')
local delta_tax_percent2 = `delta_tax_percent'*100
global delta_tax_pct_2006_`x'_`y' = `delta_tax_percent2'


gen delta_tax_08=tax_2-tax_1 if treat_i==1
sum delta_tax_08
local delta_tax_08 = abs(`r(mean)')
local delta_tax_082 = `delta_tax_08'*100

gen delta_tax_percent_08 = delta_tax_08/tax_0
sum delta_tax_percent_08
local delta_tax_percent_08 = abs(`r(mean)')
local delta_tax_percent_082 = `delta_tax_percent_08'*100
global delta_tax_pct_2008_`x'_`y' = `delta_tax_percent_082'


*  Reduced Form 
display _newline(5)
display "Reduced Form, 2 treatments (06 & 08), control group: `x'-`y'%" 
reg Inmovers DiD1 DiD2 treat_i  ib2005.zuzugjahre, vce(robust)
scalar define semi_e_06 = _b[DiD1]/`delta_tax_06'/100
scalar define semi_e_08 = _b[DiD2]/`delta_tax_08'/100

sum Inmovers if zuzugjahre <2006 & zuzugjahre >2000 & treat_i == 1
scalar define meanoutcome = `r(mean)'
global mean_incomers = meanoutcome

 outreg2  using "DiD_Incomers_inctreat_`x'_`y'_uw-2tr.xls", excel label  e(F) dec(2) replace ///
 addnote("Robust SE's in parentheses") ///
 keep(DiD1 DiD2 ib2005.zuzugjahre treat_i ) ctitle("Taxpayers", "Reduced", "(level)") title("DiD Estimations Incomers only, Control Group: `x'-`y'% of Threshold") ///
 addstat($ \Delta \tau_{2006} $ \% pts, `delta_tax_062', Semi-elasticity 2006, semi_e_06, $ \Delta \tau_{2008} $ \% pts, `delta_tax_082', Semi-elasticity 2008, semi_e_08, $ \overline{Y}_{t<2006} $ , meanoutcome)

replace Inmovers = ln(Inmovers)

sum Inmovers if zuzugjahre <2006 & zuzugjahre >2000 & treat_i == 1
scalar define lnmeanoutcome = `r(mean)'
global lnmean_incomers = lnmeanoutcome

display _newline(5)
display "Reduced Form (in logs), 2 treatments (06 & 08), control group: `x'-`y'%" 
reg Inmovers DiD1 DiD2 treat_i  ib2005.zuzugjahre, vce(robust)    
 outreg2  using "DiD_Incomers_inctreat_`x'_`y'_uw-2tr.xls", excel label  e(F) dec(3) append ///
 addnote("Robust SE's in parentheses") ///
  addstat($ \overline{Y}_{t<2006} $ , lnmeanoutcome) ///
 keep(DiD1 DiD2  ib2005.zuzugjahre treat_i ) ctitle(" ", "Reduced", "(log)") 


* 2SLS
gen lnnet_avgtt = ln(1-avgtt)
label var lnnet_avgtt "$\eta^F$"

	// HAUSMAN TEST (put this first, to include results in regression tables
	* 	Hypothesis H0: tax rates are exogenous
	*	Note:  	1) if all vars are exogenous, both 2sls and ols are consistent
	*			2) if all vars are exogenous, ols is more efficient, but it's inconsistent if violated
quiet{
	foreach var in lnnet_avgtt {
		ivreg2 Inmovers treat_i   ib2005.zuzugjahre (`var' = DiD1)   
		estimates store iv_`var'

		reg Inmovers `var' treat_i  ib2005.zuzugjahre 
		estimates store ols_`var'

		hausman iv_`var' ols_`var'
		scalar chi_`var' = r(chi2)
		scalar p_`var' = r(p)
	} 

		foreach var in lnnet_avgtt {
		display `chi_`var''
		}

		foreach var in lnnet_avgtt {
		display `p_`var''
		}
}


display _newline(5)
display "IV-Regression, 2 treatments, avg. NTR, control group: `x'-`y'%" 

ivreg2 Inmovers treat_i  ib2005.zuzugjahre (lnnet_avgtt = DiD1 DiD2) , first savefirst savefprefix(_1st2tr_`x'_`y'_) redundant(DiD1 DiD2) robust 
est store ivreg_avgttr_`x'_`y'

di e(sstatp)
di e(archi2p)
matlist e(first)

//statisttics to be saved in first-stage table:
display "first stage statistics"
// empty the locals
local Fstat
local Fstat_alt1
local Fstat_alt2
local widstat
local idstat
local idp
local esstat
local estatp
local arf
local arfp
local archi2
local archi2p
local sstat
local sstatp

// save stats as locals with nice display format
local idstat 	= trim("`: display %5.2f e(idstat)'")
local idp 		= trim("`: display %5.3f e(idp)'")
local esstat 	= trim("`: display %5.2f e(estat)'")
local estatp 	= trim("`: display %5.3f e(estatp)'")
local arf 		= trim("`: display %5.2f e(arf)'")
local arfp 		= trim("`: display %5.3f e(arfp)'")
local archi2 	= trim("`: display %5.2f e(archi2)'")
local archi2p	= trim("`: display %5.3f e(archi2p)'")
local sstat 	= trim("`: display %5.2f e(sstat)'")
local sstatp 	= trim("`: display %5.3f e(sstatp)'")
	
// F-Test
local widstat = trim("`: display %5.2f e(widstat)'") // F statistic for weak identification (Cragg-Donald or Kleibergen-Paap)
local Fstat_alt1 = trim("`: display %5.2f e(cdf)'") // should be the same as widstat, but not valid when errors are not iid! (e.g., robust)
local Fstat_alt2 = trim("`: display %5.2f e(F)'")	// overall F-test of 2nd stage
global Ftest_`x'_`y'_2tr = `widstat'
global Ftest2_`x'_`y'_2tr = `Fstat_alt1'
global Ftest3_`x'_`y'_2tr = `Fstat_alt2'

// compute p-value for F-Test widstat
	local df1 = e(ardf)
	local df2 = e(ardf_r)
	local F = e(widstat)

	display "These are the parameters for the F-Test: F( `df1', `df2', `F')"
	local pvalF = Ftail(`df1', `df2', `F')
		display "This is the F-stat pvalue for `x'-`y'% group: `pvalF'"
				gen starF = ""
				replace starF="*" in 1 if `pvalF'<.1 &`pvalF'>=.05 //print star if p-value between
				replace starF="**"  in 1 if `pvalF'<.05 &`pvalF'>=.01
				replace starF="***"  in 1 if `pvalF'<.01
				local pF = starF[1]
					display "Stars for F-stat pvalue for `x'_`y': `pF'"
		global Ftest_`x'_`y'_st_2tr = "`widstat'`pF'"
		local Ftest_pval = trim("`: display %5.3f `pvalF''")
		global Ftest_pval_`x'_`y'_2tr = `Ftest_pval'
				drop starF


 outreg2  using "DiD_Incomers_inctreat_`x'_`y'_uw-2tr.xls", excel label dec(3)  append ///
 keep(lnnet_avgtt DiD1 DiD2 ib2005.zuzugjahre treat_i ) ctitle(" ", "2SLS", "(log)")  ///
 addnote("Robust SE's in parentheses") ///
  addstat(Hausman$ ^1$ , chi_lnnet_avgtt, P-value, p_lnnet_avgtt )
  
 outreg2  using "DiD_Resultstable_Inmovers_`x'_`y'_uw.xls", excel label dec(3)  append ///
 keep( lnnet_avgtt treat_i ib2005.zuzugjahre treat_i ) ///
  sortvar(DiD lnnet_mtr lnnet_avgtt treat_i i.zuzugjahre) ctitle(" ", "2SLS", "(log)")  ///
 addnote("Robust SE's in parentheses") ///
  addstat(Hausman$ ^1$ , chi_lnnet_avgtt, P-value, p_lnnet_avgtt )
  
// REGRESSION TABLE FOR APPENDIX
  if `x' == 60 & `y' == 80 {
	outreg2 using "$mypathRR/Results/appendixtables/B4_appendixtable.xls", excel label  e(F) dec(3) append ///
	sortvar(DiD lnnet_avgtt treat_i i.zuzugjahre  ) ctitle(" ", " ", "2SLS", "(log)")  ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80}, ///
			$ \Delta \tau_{2008} $ \% pts, ${delta_tax_pct_2008_60_80}, ///
			Hausman$ ^1$ , chi_lnnet_avgtt, P-value, p_lnnet_avgtt)
  }
  
  if `x' == 55 & `y' == 75 {
  	est restore iv07_`x'_`y'
	outreg2 using "$mypathRR/Results/appendixtables/B4_appendixtable.xls", excel label  e(F) dec(3) append ///
	sortvar(DiD lnnet_avgtt treat_i i.zuzugjahre  ) ctitle(" ", "55-75 ", "2SLS", "(log)")  ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_55_75}, ///
			$ \Delta \tau_{2008} $ \% pts, ${delta_tax_pct_2008_55_75}, ///
			Hausman$ ^1$ , chi_lnnet_avgtt, P-value, p_lnnet_avgtt)
			
	est restore ivreg_avgttr_`x'_`y'
	outreg2 using "$mypathRR/Results/appendixtables/B4_appendixtable.xls", excel label  e(F) dec(3) append ///
	sortvar(DiD lnnet_avgtt treat_i i.zuzugjahre  ) ctitle(" ", "55-75 ", "2SLS", "(log)")  ///
	addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_55_75}, ///
			$ \Delta \tau_{2008} $ \% pts, ${delta_tax_pct_2008_55_75}, ///
			Hausman$ ^1$ , chi_lnnet_avgtt, P-value, p_lnnet_avgtt)
  }
  
  
 // save coefficients, standard errors and stars for final output table
 		local se_`x'_`y'_E = _se[lnnet_avgtt]
		local e_`x'_`y'_E = _b[lnnet_avgtt]
		local pval = 2*(1-normal(abs(_b[lnnet_avgtt]/_se[lnnet_avgtt])))
			display "`pval'"
			gen star="" 
			replace star="*" in 1 if `pval'<.1 &`pval'>=.05 //print star if p-value between
			replace star="**"  in 1 if `pval'<.05 &`pval'>=.01
			replace star="***"  in 1 if `pval'<.01
			local p = star[1]
				display "`p'"
		local e_`x'_`y'_E = trim("`: display %5.3f `e_`x'_`y'_E''")
		global E10_`x'_`y'_E = "`e_`x'_`y'_E'`p'"
		global SE10_`x'_`y'_E= trim("`: display %5.3f `se_`x'_`y'_E''")
			drop star

estimates restore _1st2tr_`x'_`y'_lnnet_avgtt
outreg2  using "DiD_Incomers_inctreat_first_`x'_`y'_uw-2tr.xls",  excel label replace ///
  addstat(	/*"Cragg-Donald F statistic", `Fstat_alt1', "F", `Fstat_alt2',*/ ///
   			"Weak identification F test$^a$", `widstat', "\hspace{0.5cm}\textit{P-value}", `Ftest_pval', ///
            "Underidentification test$^b$", `idstat', "\hspace{0.5cm}\textit{P-value}", `idp' , ///
			 "$\chi^2$ test  significance endog. regressors$^c$", `archi2' , "\hspace{0.5cm}\textit{P-value}", `archi2p' ,  ///
			 "S statistic significance endog. regressors$^d$", `sstat', "\hspace{0.5cm}\textit{P-value}", `sstatp') ///
			 /*"Anderson-Rubin F-test of significance of endogenous regressors", `arf', "\hspace{0.5cm}\textit{P-value}", `arfp',*/ ///
			 /*"Endogeneity test (C statistic or difference-in-Sargan stat)", `esstat', "\hspace{0.5cm}\textit{P-value}", `estatp'*/  ///
	addnote("Robust standard errors in parentheses.") ///
	keep(lnnet_avgtt DiD1 DiD2 ib2005.zuzugjahre treat_i ) ctitle("2001-10", "Inmovers", "`x'-`y'\%") 


* ols model, no IV  
display _newline(5)
display "OLS, no IV, avg. NTR, control group: `x'-`y'%"
reg Inmovers lnnet_avgtt treat_i  ib2005.zuzugjahre, vce(robust) 
 outreg2  using "DiD_Incomers_inctreat_`x'_`y'_uw-2tr.xls", excel label  e(F) dec(3)  append ///
  addnote("Robust SE's in parentheses") ///
  keep(lnnet_avgtt i.zuzugjahre treat_i ) ctitle(" ", "OLS", "(log)") 

// REGRESSION TABLE FOR APPENDIX
 if `x' == 60 & `y' == 80 {
  outreg2 using "$mypathRR/Results/appendixtables/B4_appendixtable.xls", excel label  e(F) dec(3) append ///
 sortvar(DiD lnnet_avgtt treat_i i.zuzugjahre  ) ctitle(" ", " ", "OLS", "(log)") ///
 addstat($ \Delta \tau_{2006} $ \% pts, ${delta_tax_pct_2006_60_80}, ///
			$ \Delta \tau_{2008} $ \% pts, ${delta_tax_pct_2008_60_80})
 }
			
 
est restore ivreg_avgttr_`x'_`y'
 outreg2 using "DiD_Resultstable_Inmovers_`x'_`y'_uw.xls", excel label dec(3)  append ///
 keep(lnnet_avgtt ib2005.zuzugjahre treat_i ) sortvar(DiD lnnet_mtr lnnet_avgtt treat_i i.zuzugjahre) ctitle(" ", "2SLS", "(log)")  ///
 addnote("Robust SE's in parentheses") ///
  addstat(Hausman$ ^1$ , chi_lnnet_avgtt, P-value, p_lnnet_avgtt )
  
  
restore
cap rm "DiD_Incomers_inctreat_first_`x'_`y'_uw-2tr.txt"
cap rm "DiD_Resultstable_Inmovers_`x'_`y'_uw.txt"
cap rm "DiD_Incomers_inctreat_`x'_`y'_uw-2tr.txt"
}




